<?php
namespace app\controller\admin;

use app\BaseController;
use think\facade\Request;
use app\model\PrinterModel;

/**
 * 打印机测试控制器
 */
class PrinterTest extends BaseController
{
    /**
     * 测试打印
     */
    public function test()
    {
        $printer_id = Request::param('printer_id');
        
        $printer = PrinterModel::get_info($printer_id);
        if (empty($printer)) {
            return json([
                'status' => 1,
                'description' => '打印机不存在'
            ]);
        }

        // 生成测试小票内容
        $content = $this->generate_test_receipt();

        // 发送到打印机
        $result = $this->send_to_printer($printer, $content);

        if ($result['status'] == 0) {
            return json([
                'status' => 0,
                'description' => '测试打印成功'
            ]);
        } else {
            return json([
                'status' => 1,
                'description' => '测试打印失败: ' . $result['message']
            ]);
        }
    }

    /**
     * 生成测试小票
     */
    private function generate_test_receipt()
    {
        $content = "<CB>神秘商城</CB><BR>";
        $content .= "<CB>打印机测试</CB><BR>";
        $content .= "--------------------------------<BR>";
        $content .= "测试时间: " . date('Y-m-d H:i:s') . "<BR>";
        $content .= "--------------------------------<BR>";
        $content .= "这是一张测试小票<BR>";
        $content .= "如果您看到这张小票<BR>";
        $content .= "说明打印机配置成功!<BR>";
        $content .= "--------------------------------<BR>";
        $content .= "<CB>打印机工作正常!</CB><BR>";
        $content .= "<QR>www.x-mii.com</QR>";
        
        return $content;
    }

    /**
     * 发送到打印机
     */
    private function send_to_printer($printer, $content)
    {
        if ($printer['type'] == 'feie') {
            return $this->print_feie($printer, $content);
        } else if ($printer['type'] == 'yilianyun') {
            return $this->print_yilianyun($printer, $content);
        } else {
            return [
                'status' => 1,
                'message' => '不支持的打印机类型'
            ];
        }
    }

    /**
     * 飞鹅打印机接口
     */
    private function print_feie($printer, $content)
    {
        $user = ''; // 飞鹅账号
        $ukey = $printer['printer_key'];
        $sn = $printer['printer_sn'];
        $time = time();
        
        $sign = sha1($user . $ukey . $time);
        
        $data = [
            'user' => $user,
            'stime' => $time,
            'sig' => $sign,
            'apiname' => 'Open_printMsg',
            'sn' => $sn,
            'content' => $content,
            'times' => 1
        ];
        
        $api_url = !empty($printer['api_url']) ? $printer['api_url'] : 'http://api.feieyun.cn/Api/Open/';
        
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $api_url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        
        $result = curl_exec($ch);
        curl_close($ch);
        
        $result = json_decode($result, true);
        
        if ($result['ret'] == 0) {
            return ['status' => 0, 'message' => '打印成功'];
        } else {
            return ['status' => 1, 'message' => $result['msg']];
        }
    }

    /**
     * 易联云打印机接口
     */
    private function print_yilianyun($printer, $content)
    {
        // 易联云打印接口实现
        return ['status' => 0, 'message' => '打印成功'];
    }
}
